package cn.itsource.org.service.impl;


import cn.hutool.core.date.DateTime;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.exception.ErrorCode;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.impl.BaseImpl;
import cn.itsource.basic.util.PageResult;
import cn.itsource.org.domain.TDepartment;
import cn.itsource.org.mapper.TDepartmentMapper;
import cn.itsource.org.query.TDepartmentQuery;
import cn.itsource.org.service.TDepartmentService;
import cn.itsource.system.domain.OperationLogs;
import cn.itsource.system.mapper.OperationLogsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 26871
 * @description 针对表【t_department】的数据库操作Service实现
 * @createDate 2024-04-25 10:24:45
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class TDepartmentServiceImpl extends BaseImpl<TDepartment> implements TDepartmentService {
    @Autowired
    private TDepartmentMapper tDepartmentMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private OperationLogsMapper operationLogsMapper;

    @Override
    @Transactional
    public void delete(Long id) {
        tDepartmentMapper.delete(id);
        OperationLogs operationLogs = new OperationLogs(1, "Department", "delete");
        operationLogsMapper.insert(operationLogs);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        tDepartmentMapper.patchDelete(ids);
        OperationLogs operationLogs = new OperationLogs(1, "Department", "patchDelete");
        operationLogsMapper.insert(operationLogs);
    }

    @Override
    @Transactional
    public void insert(TDepartment tDepartment) {
        System.out.println(222);
        tDepartmentMapper.insert(tDepartment);
        addDirPath(tDepartment);
        OperationLogs operationLogs = new OperationLogs(1, "Department", "insert");
        operationLogsMapper.insert(operationLogs);
    }

    @Override
    @Transactional
    public void update(TDepartment tDepartment) {
        tDepartmentMapper.update(tDepartment);
        OperationLogs operationLogs = new OperationLogs(1, "Department", "update");
        operationLogsMapper.insert(operationLogs);
    }

    //新增或者修改
    @Override
    @Transactional
    public void save(TDepartment tDepartment) {
        if (tDepartment.getId() == null) {
            insert(tDepartment);
        } else {
            if (tDepartment.getId() == tDepartment.getParentId())
                new BusinessException(ErrorCode.DEPT_SELECT_ERROR.getMessage());
            update(tDepartment);
        }
    }
    /*@Override
    public List<TDepartment> deptTree() {
        Object deptTree = redisTemplate.opsForValue().get("deptTree");
        if (deptTree!=null) {
            return (List<TDepartment>) deptTree;
        }else {
            deptTree = getDeptTreeFromDb();
            redisTemplate.opsForValue().set("deptTree",deptTree);
            return (List<TDepartment>)deptTree;
        }
    }*/

    @Override
    public List<TDepartment> deptTree() {
        //3.没有 - 调用getDeptTreeFormDb()查询数据库 - 把它放到缓存中
        List<TDepartment> deptTreefromDb = getDeptTreeFromDb();
        System.out.println(deptTreefromDb);
        return deptTreefromDb;
    }

    private List<TDepartment> getDeptTreeFromDb() {
        //首先查询出所以department并将其缓存到HashMap中
        Map<Long, TDepartment> map = new HashMap<>();
        List<TDepartment> list = tDepartmentMapper.selectAll();
        //遍历list存入map中
        for (TDepartment tDepartment : list) {
            map.put(tDepartment.getId(), tDepartment);
        }
        List<TDepartment> deptTree = new ArrayList<>();
        for (TDepartment tDepartment : list) {
            //如果父节点为空，则直接添加到根节点
            if (tDepartment.getParentId() == null) {
                deptTree.add(tDepartment);
            } else {
                //如果父节点不为空，则将父节点的子节点添加到父节点中
                TDepartment parent = map.get(tDepartment.getParentId());
                if (parent != null) {
                    parent.getChildren().add(tDepartment);
                }
            }
        }
        System.out.println(deptTree);
        return deptTree;
    }


    /**
     * 获取部门路径
     *
     * @param tDepartment
     * @return
     */
    @Override
    public void addDirPath(TDepartment tDepartment) {
        if (tDepartment.getParentId() == null) {
            tDepartment.setDirpath("/" + tDepartment.getId());
        } else {
            TDepartment parent = tDepartmentMapper.selectById(tDepartment.getParentId());
            tDepartment.setDirpath(parent.getDirpath() + "/" + tDepartment.getId());
        }
    }
}
